package com.first.group.authserver.service.impl;

import com.first.group.authserver.entity.AddressEntity;
import com.first.group.authserver.service.AddressService;
import com.first.group.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.first.group.common.utils.PageUtils;
import com.first.group.common.utils.Query;

import com.first.group.authserver.dao.UserDao;
import com.first.group.authserver.entity.UserEntity;
import com.first.group.authserver.service.UserService;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private AddressService addressService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R saveUser(UserEntity user) {

        //添加用户
        //主要是判断地址的问题
        getAddressName(user);

        baseMapper.insert(user);
        return R.ok();
    }

    @Override
    public R updateUser(UserEntity user) {

        getAddressName(user);

        baseMapper.updateById(user);
        return R.ok();
    }

    //获取地址名称
    private void getAddressName(UserEntity user) {
        String addressName = "";
        String[] split = user.getUserAddressId().split(",");
        for (String s : split) {
            AddressEntity byId = addressService.getById(new Long(s));
            addressName += "/" + byId.getAddressName();
        }
        String substring = addressName.substring(1);
        user.setAddressName(substring);
    }

}